<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>3、ECMAScript基础(深入)之数组基础&数组方法&类数组</title>
</head>
<body>
    <script>
        
        // concat
        var arr1 = ['a', 'b'];
        var arr2 = ['d'];
        var arr = arr1.concat(arr2);
        console.log(arr);
        console.log('-----分割线-----');

        // toString(数组转字符串)
        var arr3 = ['a', 'b', 'c'];
        var arr4 = arr3.toString();
        console.log(arr4);

        console.log('-----分割线-----');

        // slice splice 类似
        var arr5 = [{a: 1}, {b: 2}, {c: 3}];
        var arr6 = arr5.slice(); // 数组深克隆
        arr6.push({d: 4});
        console.log(arr5);
        console.log(arr6);

        // 闭区间[]   开区间() 
        
        // [+start, +end)
        // var arr7 = arr5.slice(1);
        // console.log(arr7);
        
        // [+start, -end)
        // [{a: 1}, {b: 2}, {c: 3}, {d: 4}]
        // var arr7 = arr6.slice(1, -1)
        // console.log(arr7);

        // [-start, -end)
        // [{a: 1}, {b: 2}, {c: 3}, {d: 4}]
        var arr7  = arr6.slice(-3, -1);
        console.log(arr7);

        // join 数组转字符串
        var arr8 = ['a', 'b', 'c', 'd', 'e'];
        // var str = arr8.join('');
        var str = arr8.join();
        var str2 = arr8.toString();
        console.log(str);
        console.log(str2);

        //split 字符串转数组
        var arr9 = str2.split(',');
        console.log(arr9);
        var arr10 = str.split(',', 2); // 2表示长度
        console.log(arr10);

        /**
         * 类数组
         * 类似于数组的对象
         * **/
        function test(){
            console.log(arguments);
        }
        test(1, 2, 3, 4, 5);

        // 重写push
        // Array.prototype.push = function(elem) {
        //     this[this.length] = elem;
        //     this.length++;
        // }

        // 阿里笔试
        var obj = {
            '2': 3,
            '3': 4,
            'length': 2,
            'splice': Array.prototype.splice,
            'push': Array.prototype.push
        }

        obj.push(1); // 2: 1
        obj.push(2); // 3: 2
        console.log(obj); // [,,1,2]

        // 练习
        var person = {
            '0': '张小一',
            '1': '张小二',
            '2': '张小三',
            'name': '张小三',
            'age': 32,
            'weight': 140,
            'height': 180,
            'length': 3
        }
        Object.prototype.push = Array.prototype.push;
        Object.prototype.splice = Array.prototype.splice;

        for(var key in person){
            if(person.hasOwnProperty(key)){
                console.log(person[key]);
            }
        }
</script>
</body>
</html>